<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>后台管理模板</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no">
		<link rel="stylesheet" href="../../layui/css/layui.css" media="all" />
		<link rel="stylesheet" href="../../css/global.css" media="all">
		<link rel="stylesheet" type="text/css" href="http://www.jq22.com/jquery/font-awesome.4.6.0.css">
		<link rel="stylesheet" href="../../datatables/media/css/jquery.dataTables.css">
		<link rel="stylesheet" href="../../datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.css">
	</head>
	<body>
		<div class="layui-layout layui-layout-admin" style="border-bottom: solid 5px #1aa094;">
			<!--头部导航-->
			<div class="layui-header header header-demo" ></div>
			<!--左侧导航-->
			<div class="layui-side layui-bg-black" id="admin-side">
			</div>
			<!--右侧具体内容-->
			<div class="layui-body" style="bottom: 0;border-left: solid 2px #1AA094;padding:20px" id="admin-body">
				<div>
					<label class="title">管理员账号管理</label>
					<a href="addmanager.html"><button class="layui-btn layui-btn-small" style="float: right;margin-bottom: 10px"><i class="layui-icon">&#xe608;</i> 添加</button></a>
				</div>
				<hr />
				<div class="select">
				<select name="managerselect" lay-verify="" id="managerSequence">

					<option value="1">按编号↑</option>
					<option value="2">只看超级管理员</option>
					<option value="3">只看管理员</option>
					<option value="4">按创建时间↑</option>
					<option value="5">按创建时间↓</option>
				</select>

				</div>
				<div class="layui-form">
                    <table class="layui-table" id="managerTable">
                        <thead>
                            <tr>
                            <th><input type="checkbox" name="" id="checkall" onchange="checkAll(this,'#managerTable')"></th>
								<th style="width: 80px">头像</th>
								<th>编号</th>
								<th>用户名</th>
								<th>类型</th>
								<th>性别</th>
								<th>创建时间</th>
								<th>状态</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
							<tr>
								<td><input type="checkbox" name="managercheck" id="" ></td>
								<td><img src="../../images/0.jpg"></td>
								<td>0412</td>
								<td>lzy</td>
								<td>超级管理员</td>
								<td>女</td>
								<td>2017-6-30</td>
								<td>使用中</td>
								<td><div class="layui-btn layui-btn-danger  layui-btn-small managerclose">冻结账号</div><div class="layui-btn  layui-btn-small reset_psw">重置密码</div><div class="layui-btn layui-btn-warm  layui-btn-small item_edit">编辑</div></td>
							</tr>
							<tr>
								<td><input type="checkbox" name="managercheck" id="" ></td>
								<td><img src="../../images/xiaosong.jpg"></td>
								<td>042</td>
								<td>adj</td>
								<td>超级管理员</td>
								<td>男</td>
								<td>2017-6-30</td>
								<td>使用中</td>
								<td><div class="layui-btn layui-btn-danger  layui-btn-small managerclose">冻结账号</div><div class="layui-btn  layui-btn-small reset_psw">重置密码</div><div class="layui-btn layui-btn-warm  layui-btn-small item_edit">编辑</div></td>
							</tr>
							<tr>
								<td><input type="checkbox" name="managercheck" id="" ></td>
								<td><img src="../../images/1.jpg"></td>
								<td>0412</td>
								<td>zal</td>
								<td>超级管理员</td>
								<td>女</td>
								<td>2017-7-1</td>
								<td>使用中</td>
								<td><div class="layui-btn layui-btn-danger  layui-btn-small managerclose">冻结账号</div><div class="layui-btn  layui-btn-small reset_psw">重置密码</div><div class="layui-btn layui-btn-warm  layui-btn-small item_edit">编辑</div></td>
							</tr>
						</tbody>
					</table>
                    <div class="closeall">
                    <div class="layui-btn layui-btn-danger  layui-btn-small" id="managercloseall">批量冻结</div><div class="layui-btn  layui-btn-small " id="managerresetall">重置密码</div></div>
					</div>
            </div>
            <!--底部导航-->
            <div class="layui-footer footer footer-demo" id="admin-footer"></div>
            <!--JS模块-->
            <script type="text/javascript" src="../../layui/layui.js"></script>
            <script src="../../js/jquery-1.12.3.min.js"></script>
            <script src="../../js/index.js"></script>
            <script type="text/javascript" src="../../js/jquery.cookie.js"></script>
            <!-- DataTables JavaScript -->
            <script src="../../datatables/media/js/jquery.dataTables.min.js"></script>
            <script>

            var data = {
                "code": 200,
                "admins": [{
                    "adminId":12345,
                    "adminUrl": "../../images/find_1.png",
                    "adminName": "0412",
                    "adminNickname": "zylin",
                    "adminPower": 1,
                    "adminSex": "女",
                    "adminRegisterTime": "2017-7-7",
                    "adminState": 1
                        // "adminPwd": "SO25KtpgBz",
                }, {
                    "adminId":12345,
                    "adminUrl": "../../images/demo1.png",
                    "adminName": "0412",
                    "adminNickname": "zylin",
                    "adminPower": 1,
                    "adminSex": "女",
                    "adminRegisterTime": "2017-7-7",
                    "adminState": 1
                }, {
                    "adminId":12345,
                    "adminUrl": "../../images/1.jpg",
                    "adminName": "0412",
                    "adminNickname": "zylin",
                    "adminPower": 1,
                    "adminSex": "女",
                    "adminRegisterTime": "2017-7-7",

                    "adminState": 0
                }]
            }

            $(function() {
                $("#manager").addClass('layui-this'); //对应订单管理的背景色
                $('#managerTable').DataTable({
                    responsive: true,
                    ordering: false,
                    searching: true,
                    "bLengthChange": false,
                    "bRetrieve": true,
                    "bFilter": true //过滤功能
                });

                managerSelectChange(1)
                adminCookie()
            })


           
            
                    
            $(document).on('click', 'tr td:not(:first)', function() {
                if ($(this).siblings().first().children().is(':checked') == false) {
                    $(this).siblings().first().children().prop("checked", true);
                } else {
                    $(this).siblings().first().children().prop("checked", false);
                }
            })

            $(document).on('click', '.item_edit', function() {
                var adminId = this.id;
                window.location.href = "editmanager.html?adminId=" + adminId;
            })

            $(document).on('click', '.managerclose', function() {
                k = $(this).parents("tr").index() + 1;
                var adminNickname = $(this).parents("tr").children('td:nth-child(4)').text()
                var adminid = this.id
                layui.use('layer', function() {
                    var layer = layui.layer;

                    layer.confirm('是否要冻结管理员：' + adminNickname + "?", {
                        icon: 2,
                        title: '提示'
                    }, function(index) {
                        /*$.ajax({
                            type: "DELETE",
                            url: "/randomdelivery/AdminController/closeAdmin/"+adminid,
                            data: {
                            },
                            async: true,

                            dataType: "json",
                            contentType: "application/json",
                            success: function(data) {*/
                        if (data.code == 200) {
                            $("table#managerTable").find("tr:eq(" + k + ")").children('td:nth-child(8)').text("已冻结").css('color', 'red');
                            $("table#managerTable").find("tr:eq(" + k + ")").children('td:nth-child(9)').children('.managerclose').text("激活账号").attr('class', 'layui-btn layui-btn-normal  layui-btn-small wakeup');
                            $("input[type='checkbox']:checked").prop('checked', false)
                            layer.close(index);
                        }
                            /*},
                            Error: function() {
                                alert("服务器出错");
                            }
                    });*/
                    });


                });
            })

            $(document).on('click', '.wakeup', function() {
                k = $(this).parents("tr").index() + 1;
                var managerNickname = $(this).parents("tr").children('td:nth-child(4)').text()
                var managerid = this.id
                layui.use('layer', function() {
                    var layer = layui.layer;
                    layer.confirm('是否要激活：' + managerNickname + "?", {
                        icon: 1,
                        title: '提示'
                    }, function(index) {

                        /*$.ajax({
                            type: "DELETE",
                            url: "/randomdelivery/AdminController/openAccount/"+managerid,
                            data: {
                            },
                            async: true,
                            dataType: "json",
                            contentType: "application/json",
                            success: function(data) {*/
                        if (data.code == 200) {
                            $("table#managerTable").find("tr:eq(" + k + ")").children('td:nth-child(8)').text("使用中").css('color', 'black');
                            $("table#managerTable").find("tr:eq(" + k + ")").children('td:nth-child(9)').children('.wakeup').text("冻结账号").attr('class', 'layui-btn layui-btn-danger  layui-btn-small managerclose');
                            $("input[type='checkbox']:checked").prop('checked', false)
                        layer.close(index);
                        }
                            /*},
                            Error: function() {
                                alert("服务器出错");
                            }*/
                    });

                });

            })

            $(document).on('click', '.reset_psw', function() {
                var adminNickname = $(this).parents("tr").children("td:nth-child(3)").text();
                var adminId = this.id
                layui.use('layer', function() {
                    var layer = layui.layer;
                    layer.confirm('是否要重置管理员：' + adminNickname + "的密码？", {
                        icon: 3,
                        title: '提示'
                    }, function(index) {

                        /*$.ajax({
                                    url: 'randomdelivery/resetPassword/'+adminId,
                                    type: 'GET',
                                    dataType: 'json',
                                    contentType: "application/json",
                                    data: {},

                                    success: function(data) {*/

                        if (data.code == 200) {
                            layer.msg("重置成功")
                            $("input[type='checkbox']:checked").prop('checked', false)
                            layer.close(index);
                        }
                        /*},
                                error: function() {
                                    alert("服务器出错");
                                }
                            });*/
                    });

                });

            })

            $("#managercloseall").on('click', function() {

                // var arr
                if ($("input[name='managercheck']:checked").length == 0) {
                    layer.msg("请选择管理员")
                } else {
                    layui.use('layer', function() {
                        var layer = layui.layer;

                        layer.confirm('是否要冻结选中管理员？', {
                            icon: 2,
                            title: '提示'
                        }, function(index) {
                            $("input[name='managercheck']:checked").each(function() {
                                var element = this;
                                if (!isNaN(element.id)) {
                                    // m = $(this).parents("tr").index() + 1; // 获取checkbox所在行的顺序

                                    /*$.ajax({
                                        type: "DELETE",
                                        url: "/randomdelivery/AdminController/closeAccount/"+,
                                        data: {
                                        },
                                        async: true,
                                        dataType: "json",
                                        contentType: "application/json",
                                        success: function(data) {*/
                                    if (data.code == 200) {
                                        $(element).parents("tr").children('td:nth-child(8)').text("已冻结").css('color', 'red')
                                        $(element).parents("tr").children('td:nth-child(9)').children('.managerclose').text("激活账号").attr('class', 'layui-btn layui-btn-normal  layui-btn-small wakeup');
                                        $("input[type='checkbox']:checked").prop('checked', false)
                                    }
                                    /*},
                                Error: function() {
                                    alert("服务器出错");
                                }
                            });*/
                                }
                            })
                            layer.close(index);
                        });
                    });
                }
            });

             $("#managerresetall").on('click', function() {
                 if ($("input[name='managercheck']:checked").length == 0) {
                     layer.msg("请选择管理员")
                 } else {
                     layui.use('layer', function() {
                         var layer = layui.layer;
                         layer.confirm('是否要重置选中管理员密码？', {
                             icon: 3,
                             title: '提示'
                         }, function(index) {
                             $("input[name='managercheck']:checked").each(function() {
                                 var element = this;

                                 // m = $(this).parents("tr").index() + 1; // 获取checkbox所在行的顺序
                                 /*$.ajax({
                                     type: "DELETE",
                                     url: "/randomdelivery/resetPassword/"+element.id,
                                     data: {
                                     },
                                     async: true,
                                     dataType: "json",
                                     contentType: "application/json",
                                     success: function(data) {*/
                                 if (data.code == 200) {
                                     $("input[type='checkbox']:checked").prop('checked', false)
                                 }
                                 /*},
                                Error: function() {
                                    alert("服务器出错");
                                }
                            });*/
                             })
                             layer.msg("重置成功")
                             layer.close(index);
                         });
                     });
                 }
             });


            $("#managerSequence").bind("change", function() {
                managerSelectChange($(this).val())

                alert(213)
            })

            function managerSelectChange(v) {
                /* $.ajax({
                     type: "get",
                     url: "/randomdelivery/AdminController/admins/pageCount/1/pageSize/10",
                     data: {
                         "sequence": v,
                     },
                     async: true,

                     dataType: "json",
                     contentType: "application/json",
                     success: function(data) {
    
                     },
                     Error: function() {
                         alert("服务器出错");
                     }

                 })*/
                managerdata(data);
            }

            function managerdata(data) {
                $('#managerTable').dataTable().fnClearTable(); //清除表格内
                $.each(data.admins, function(index, item) {
                    var power
                    if (item.adminPower == 1) {
                        power = "超级管理员"
                    } else {
                        power = "普通管理员"
                    }

                    if (item.adminState ==1){
                        adminState="使用中"
                    } else {
                        adminState = "已冻结"
                    }
                    $('#managerTable').dataTable().fnAddData([
                        '<input type="checkbox" name="managercheck" id="' + item.adminId + '" >',
                        '<img src=' + item.adminUrl + '>',
                        item.adminName,
                        item.adminNickname,

                        item.adminPower,
                        item.adminSex,
                        item.adminRegisterTime,
                        adminState,
                        '<div class="layui-btn layui-btn-danger  layui-btn-small managerclose" id="' + item.adminId + '">冻结账号</div><div class="layui-btn  layui-btn-small reset_psw" id="' + item.adminId + '">重置密码</div><div class="layui-btn layui-btn-warm  layui-btn-small item_edit" id="' + item.adminId + '">编辑</div>'
                    ]);
                    if(item.adminState ==0){
                        $("table#managerTable").find("tr:eq(" + (index+1) + ")").children('td:nth-child(8)').text("已冻结").css('color', 'red');
                            $("table#managerTable").find("tr:eq(" + (index+1) + ")").children('td:nth-child(9)').children('.managerclose').text("激活账号").attr('class', 'layui-btn layui-btn-normal  layui-btn-small wakeup');
                    }
                })
            }
            		// $('#managerTable').dataTable().fnClearTable();//清除表格内

            </script>
            
        </div>
    </body>
</html>

